<template>
	<view class="wrap">
		<view class="filter-box">
			<view class="b-item" v-for="(item, index) in groupList" :key="index"
				:class="{'f-active': index == groupIndex}" @click="onGroupSelect(index)">
				<view class="s-tit">{{item.name}}</view>
			</view>
		</view>
		<view class="trade-list" v-if="groupList[groupIndex].value === 'trade' && recordList.length > 0">
			<view class="trade-item" v-for="(item, index) in recordList" :key="index">
				<view class="b-main">
					<view class="s-title">{{item.action_text}}</view>
					<view class="s-amount">{{item.amount > 0 ? '+' : ''}}{{item.amount}}</view>
				</view>
				<view class="b-info">
					<view class="s-time">{{timeFormat(item.created_at)}}</view>
					<view class="s-left">余额: {{item.after_amount}}</view>
				</view>
			</view>
		</view>
		<view class="order-list" v-if="groupList[groupIndex].value === 'order' && recordList.length > 0">
			<view class="order-item" v-for="(item, index) in recordList" :key="index">
				<view class="b-main">
					<view class="s-title">{{item.fund_pack_name}}</view>
					<view class="s-amount">{{item.fund_pack_amount}}</view>
				</view>
				<view class="b-info">
					<view class="s-time">{{timeFormat(item.payment_time)}}</view>
					<view class="s-left">实付: {{item.payment_rel}}</view>
				</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText" color="#888888"></load-more>
	</view>
</template>

<script>
	import {
		getTradeList,
		getOrderList
	} from '@/service/api/fund.js'
	import Util from '@/common/util'
	import mixinsCommon from '@/mixins/common.js'
	export default {
		mixins: [mixinsCommon],
		data() {
			return {
				groupList: [{
					name: '交易明细',
					value: 'trade'
				}, {
					name: '充值订单',
					value: 'order'
				}, ],
				groupIndex: 0,
				recordList: [],
				hasLoad: false,
				refreshTime: 0,
				loadingType: 1,
				loadingText: '',
			}
		},
		onLoad(options) {
			this.loadRecordList(true)
		},
		onReachBottom() {
			if (this.loadingType !== 1 && this.loadingType !== 2) {
				this.loadRecordList()
			}
		},
		methods: {
			loadRecordList(refresh) {
				if (this.isLogin) {
					if (this.groupList[this.groupIndex].value === 'trade') {
						this.loadTradeList(refresh)
					} else if (this.groupList[this.groupIndex].value === 'order') {
						this.loadOrderList(refresh)
					}
				} else {
					this.loadingType = 2
					this.loadingText = '暂无记录'
				}
			},
			loadTradeList(refresh) {
				console.log('loadTradeList', refresh);
				if (refresh) {
					this.page = 1;
					this.recordList = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				getTradeList(this.page, 10).then(([err, res]) => {
					console.log('getTradeList', err, res)
					this.loadingType = -1
					this.hasLoad = true
					if (!err) {
						if (res.items.length > 0) {
							this.recordList = [...this.recordList, ...res.items]
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无记录'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 2
					}
				})
			},
			loadOrderList(refresh) {
				console.log('loadOrderList', refresh);
				if (refresh) {
					this.page = 1;
					this.recordList = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				getOrderList(this.page, 10).then(([err, res]) => {
					console.log('getOrderList', err, res)
					this.loadingType = -1
					this.hasLoad = true
					if (!err) {
						if (res.items.length > 0) {
							this.recordList = [...this.recordList, ...res.items]
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无记录'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 2
					}
				})
			},
			onGroupSelect(index) {
				this.groupIndex = index
				this.loadRecordList(true)
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {
		padding-bottom: 150upx;
	}

	.filter-box {
		padding: 30upx 50upx 0 50upx;
		margin-bottom: 30upx;
		background: #fff;
		// border-bottom: 1upx solid #eee;
		display: flex;
		align-items: center;
		justify-content: center;

		.b-item {
			margin: 0 40upx;
			height: 80upx;
			line-height: 80upx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 30upx;
			padding-bottom: 6upx;
			position: relative;

			&.f-active {
				padding-bottom: 0;
				border-bottom: 6upx solid $pq-active-color;

				.s-tit {
					// color: #FF9626;
				}
			}

			.s-tit {
				color: $pq-main-color;
			}
		}
	}

	.trade-list {
		background: #fff;

		.trade-item {
			padding: 30upx;
			border-bottom: 1upx solid #eee;

			&:last-child {
				border-bottom: none;
			}

			.b-main {
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: $pq-main-color;

				.s-title {
					font-size: 28upx;
				}

				.s-amount {
					font-size: 32upx;
				}
			}

			.b-info {
				margin-top: 20upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #808080;
				font-size: 26upx;

				.s-time {}

				.s-left {}
			}
		}
	}

	.order-list {
		background: #fff;

		.order-item {
			padding: 30upx;
			border-bottom: 1upx solid #eee;

			&:last-child {
				border-bottom: none;
			}

			.b-main {
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #333;

				.s-title {
					font-size: 28upx;
				}

				.s-amount {
					font-size: 32upx;
				}
			}

			.b-info {
				margin-top: 20upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #999;
				font-size: 26upx;

				.s-time {}

				.s-left {}
			}
		}
	}
</style>